<template>
	<view class="orderList">
		<headertop title="我的订单" str="orderlist" str1="oridei" :back1="back1"></headertop>
		<view class="orderListback"></view>
		<view class="" :style="{'margin-top':statusBarHeight+'px','width':'100%','height':barHeight+'px'}">
		</view>
		<view class="seachbox">
			<image :src="_static('/images/Frame@2x(20).png')" class="lefticon" mode="widthFix">
			</image>
			<input type="text" v-model="query.keywords" placeholder="请输入关键词进行搜索" />
			<view class="seachboxbtn" @click="init('搜索')">
				搜索
			</view>
			<image :src="_static('/images/removeicon.png')" v-if="query['keywords']" @click="closeinput"
				class="closeicon" mode="" />
		</view>

		<scroll-view :scroll-left="leftnumstr" scroll-x="true" class="classifybox">
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.type=='all'?'itembox activecolor':'itembox'" @click="qiehuan('all')">
					<span>全部</span>
					<view class="activebox" v-if="query.type=='all'">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.type=='unpaid'?'itembox activecolor':'itembox'" @click="qiehuan('unpaid')">
					<span>待付款</span>
					<view class="activebox" v-if="query.type=='unpaid'">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.type=='nosend'?'itembox activecolor':'itembox'" @click="qiehuan('nosend')">
					<span>待发货</span>
					<view class="activebox" v-if="query.type=='nosend'">

					</view>
				</view>
			</view>
			<!-- 		<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.type=='nohexiao'?'itembox activecolor':'itembox'" @click="qiehuan('nohexiao')">
					<span>待核销</span>
					<view class="activebox" v-if="query.type=='nohexiao'">

					</view>
				</view>
			</view> -->
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.type=='noget'?'itembox activecolor':'itembox'" @click="qiehuan('noget')">
					<span>待收货</span>
					<view class="activebox" v-if="query.type=='noget'">

					</view>
				</view>
			</view>
			<view style="display: inline-block;margin-right: 50rpx;">
				<view :class="query.type=='completed'?'itembox activecolor':'itembox'" @click="qiehuan('completed')">
					<span>已完成</span>
					<view class="activebox" v-if="query.type=='completed'">

					</view>
				</view>
			</view>
		</scroll-view>
		<view class="itemboxss">
			<view class="msgicon" v-if="!list.length">
				<image :src="_static('/images/wxnone.png')" mode="widthFix">
				</image>
				<view class="msgtext">
					暂无订单
				</view>
			</view>
			<view class="itembox" v-for="(item,index) in  list" :key="index" @click="gopage('订单详情',item.id)">
				<view class="itembox_center">
					<view class="topline">
						<view class="dingdanbinhao">
							<!-- 	<image :src="_static('/images/Frame@2x(21).png')" class="dingdanbinhao_icon" mode="">
							</image> -->
							<span>订单编号：{{item.order_sn}}</span>
						</view>
						<view class="statustext">
							{{item.status_text}}
						</view>
					</view>
					<view class="spinfo" v-for="ite in  item.items" :key="ite.id" v-if="item.items">
						<image :src="_static(ite.goods_image)" mode=""></image>
						<view class="text1">
							<view class="l1">
								<view class="bt1">{{ite.goods_title}}</view>
							</view>
							<view class="yajinbox" v-if="ite.goods_sku_text">
								<span v-if="skufun(ite.goods_sku_text).length>1"><span
										style="font-size: 24rpx;color: #7F7F7F;">{{skufun(ite.goods_sku_text)[0]}} |
										{{skufun(ite.goods_sku_text)[1]}}</span></span>
								<span v-else><span
										style="font-size: 24rpx;color: #7F7F7F;">{{skufun(ite.goods_sku_text)[0]}}</span></span>
							</view>
							<view style="height: 32rpx;margin-top:15rpx;" v-else>

							</view>
							<view class="yajinbox">
								<span>x{{ite.goods_num}}</span>
								<view class="tihuofs">
									￥{{ite.goods_price}}
								</view>
							</view>
							<view style="width: 100%;display: flex;justify-content: flex-end; margin-top: 30rpx;">
								<view class="btnaf" v-if="ite.aftersale_status!=0&&statustext(item.status_code)=='待收货'"
									@click="gopage(textsutest(ite.aftersale_status),ite,item)">
									{{textsutest(ite.aftersale_status)}}
								</view>
							</view>
						</view>
					</view>
					<view style="width: 100%;display: flex;justify-content: flex-end;font-size: 24rpx;color: #7F7F7F;">
						<view>运费：{{item.dispatch_amount}}</view>
						<view style="margin-left:30rpx">合计：{{item.order_amount}}</view>
					</view>
					<view class="bottomline" v-if="statustext(item.status_code)=='待付款'">
						<view class="" style="display: flex;">
							<view class="borderbtn" @click.stop="qxdd(item)">
								取消订单
							</view>
							<view class="btn" style="margin-left: 28rpx;" @click.stop="ljzf(item)">
								立即支付
							</view>
						</view>
					</view>
					<view class="bottomline" v-if="statustext(item.status_code)=='待发货'">
						<view class="btn" @click.stop="sqtk(item)">
							申请退款
						</view>
					</view>
					<view class="bottomline" v-if="statustext(item.status_code)=='待收货'">
						<view class="borderbtn" @click.stop="chakanwuliu(item)">
							查看物流
						</view>
						<view class="btn" style="margin-left: 28rpx;" @click.stop="qrsh(item)">
							确认收货
						</view>
					</view>
					<view class="bottomline"
						v-if="statustext(item.status_code)!='待收货'&&statustext(item.status_code)!='待发货'&&statustext(item.status_code)!='待付款'">
						<view class="btn">
							查看详情
						</view>
					</view>

					<!-- 	<view style="height: 22rpx;"
						v-if="statustext(item.status_code)=='已取消'||statustext(item.status_code)=='已完成'||statustext(item.status_code)=='退款成功'">

					</view> -->
				</view>
			</view>
		</view>
		<up-popup :show="show" mode="center" :zoom="false">
			<view class="zsjm" v-if="typestr=='物流'">
				<image src="https://chunchi.oss-cn-beijing.aliyuncs.com/static/Frame@2x (9).png" class="closeicon"
					mode="" @click="show=false" />
				<view class="forms">
					<view class="linebox" style="margin-top: 50rpx;">
						<view class="title1">
							快递公司：
						</view>
						<view class="input">
							<input type="text" v-model="itemobj.express.express_name" placeholder="请选择"
								:disabled="true" />
						</view>
					</view>
					<view class="linebox">
						<view class="title1">
							快递单号：
						</view>
						<view class="input">
							<input type="text" v-model="itemobj.express.express_no" :disabled="true"
								placeholder="请输入联系人" />
						</view>
					</view>
				</view>
				<view class="qdbox" @click="goResult(itemobj.express.express_no)">
					复制快递单号
				</view>
			</view>
		</up-popup>
	</view>
</template>

<script setup>
	import {
		onPageScroll,
		onLoad,
		onShow,
		onReachBottom
	} from "@dcloudio/uni-app";
	import {
		ref,
		reactive,
		onMounted
	} from 'vue'
	import {
		_static
	} from '@/ulm/utils/url.js'
	import ulm, {
		$api
	} from '@/ulm';
	import headertop from '@/components/header.vue'
	const statusBarHeight = ref('')
	const leftnumstr = ref('')
	const barHeight = ref('')
	const back1 = ref('')
	const show = ref(false)
	const itemobj = ref({})
	const typestr = ref('')
	const lastpage = ref(0)
	const list = ref([])
	const query = reactive({
		page: 1,
		limit: 10,
		keywords: '',
		type: 'all'
	})
	onLoad((opt) => {
		if (opt.type) {
			query.type = opt.type
		}
	})

	onShow(() => {
		query.page = 1
		list.value = []
		init()
	})
	onPageScroll((e) => {

		if (e.scrollTop > 100) {
			back1.value = '#81D6FF'
		} else {
			back1.value = ''
		}

	})
	onMounted(() => {
		statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
		const {
			top,
			height
		} = wx.getMenuButtonBoundingClientRect();
		barHeight.value = height ? height + (top - statusBarHeight.value) * 2 : 38;
	})
	onReachBottom(() => {
		if (lastpage.value > query.page) {
			query.page++
			init()
		}
	})

	function qrsh(item) {
		uni.showModal({
			title: '提示', // 标题（可选）
			content: '是否确认收货？', // 内容（可选）
			confirmText: '确定', // 确认按钮文字（可选，默认“确定”）
			cancelText: '取消', // 取消按钮文字（可选，默认“取消”）
			success: (res) => {
				if (res.confirm) {
					sh(item.id)
					// 执行确定后的逻辑
				} else if (res.cancel) {
					// 执行取消后的逻辑
				}
			},
			fail: (err) => {
				console.error('弹窗调用失败', err);
			}
		});
	}

	async function sh(id) {
		const res = await $api.confirm({
			id
		})
		if (res.code == 1) {
			setTimeout(() => {
				uni.showToast({
					title: '确认收货成功'
				})
			}, 500)
			query.page = 1
			list.value = []
			init()
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}

	function goResult(str) {
		uni.setClipboardData({
			data: str, // 需要复制的内容
			success: () => {
				// 成功提示框显示 1 秒钟
				uni.showToast({
					title: '复制成功',
					icon: 'success',
					duration: 1000
				});
			},
			fail: () => {
				// 失败提示框显示 1 秒钟
				uni.showToast({
					title: '复制失败，请重试',
					icon: 'none',
					duration: 1000
				});
			}
		});
	}

	function chakanwuliu(item) {
		uni.navigateTo({
			url: '/sub_my/my/wuliuxinxi?id=' + item.id
		})
	}

	function sqtk(item) {
		uni.showModal({
			title: '提示', // 标题（可选）
			content: '是否申请退款？', // 内容（可选）
			confirmText: '确定', // 确认按钮文字（可选，默认“确定”）
			cancelText: '取消', // 取消按钮文字（可选，默认“取消”）
			success: (res) => {
				if (res.confirm) {
					sstt(item)
					// 执行确定后的逻辑
				} else if (res.cancel) {
					// 执行取消后的逻辑
				}
			},
			fail: (err) => {
				console.error('弹窗调用失败', err);
			}
		});
	}
	async function sstt(item) {
		const res = await $api.applyRefund({
			id: item.id
		})
		if (res.code == 1) {
			query.page = 1
			list.value = []
			init()
			setTimeout(() => {
				uni.showToast({
					title: '申请退款成功'
				})
			}, 500)
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}


	function gopage(str, id, item) {
		if (str == '订单详情') {
			uni.navigateTo({
				url: '/sub_shoppingmal/index/orderdetail?id=' + id
			})
		} else if (str == '售后详情' || str == '售后完成') {
			uni.navigateTo({
				url: '/sub_shoppingmal/index/orderdetail?typestr=shouhou&id=' + id['ext']['aftersale_id']
			})
		} else if (str == '申请售后') {
			uni.navigateTo({
				url: '/sub_shoppingmal/index/sqth?idd=' + id['id'] + '&id=' + item.id
			})
		}
	}

	function skufun(str) {
		if (!str) {
			return []
		}
		return str.split(',')
	}


	async function init(str) {
		if (str == '搜索') {
			query.type = "all"
			query.page = 1
			list.value = []
		}
		console.log(query, 'queryqueryqueryquery');
		const res = await $api.orderlist(query)
		if (res.code == 1) {
			list.value.push(...res.data.data)
			lastpage.value = res.data.last_page
		}

	}

	function statustext(str) {
		if (str == 'unpaid') {
			return '待付款'
		} else if (str == 'nosend') {
			return '待发货'
		} else if (str == 'noget') {
			return '待收货'
		} else if (str == 'nohexiao') {
			return '待核销'
		} else if (str == 'completed') {
			return '已完成'
		} else {
			if (str == 'refund_agree') {
				return '退款成功'
			}
			return '已取消'
		}
	}

	function qxdd(item) {
		uni.showModal({
			title: '提示', // 标题（可选）
			content: '是否取消订单？', // 内容（可选）
			confirmText: '确定', // 确认按钮文字（可选，默认“确定”）
			cancelText: '取消', // 取消按钮文字（可选，默认“取消”）
			success: (res) => {
				if (res.confirm) {
					qxdd1(item)
					// 执行确定后的逻辑
				} else if (res.cancel) {
					// 执行取消后的逻辑
				}
			},
			fail: (err) => {
				console.error('弹窗调用失败', err);
			}
		});
	}

	async function qxdd1(item) {
		const res = await $api.cancelorder({
			id: item.id
		})
		if (res.code == 1) {
			query.page = 1
			list.value = []
			init()
			setTimeout(() => {
				uni.showToast({
					title: '取消成功'
				})
			}, 500)

		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}

	function qiehuan(str) {
		if (str != 'all') {
			query.keywords = ''
		}
		query.type = str
		query.page = 1
		list.value = []
		init()
	}

	function closeinput() {
		query.keywords = ''
		query.type = 'all'
		query.page = 1
		list.value = []
		init()
	}


	async function ljzf(item) {
		const res1 = await $api.payPrepay({
			order_sn: item.order_sn,
			payment: 'wechat'
		})
		if (res1.code == 1) {
			var payobj = res1.data.pay_data
			uni.requestPayment({
				timeStamp: String(payobj.timeStamp),
				nonceStr: payobj.nonceStr,
				package: payobj.package,
				signType: payobj.signType,
				paySign: payobj.paySign,
				success(res) {
					setTimeout(() => {
						uni.showToast({
							title: '支付成功',
							icon: 'none'
						})
					}, 500)
					query.page = 1
					list.value = []
					init()
				},
				fail(e) {
					setTimeout(() => {
						uni.showToast({
							title: '支付失败',
							icon: 'none'
						})
					}, 500)
				}
			})
		}
	}

	function textsutest(num) {
		if (num == 0) {
			return '申请售后'
		} else if (num == 2) {
			return '售后完成'
		} else {
			return '售后详情'
		}
	}
</script>

<style lang="scss">
	.btnaf {
		width: 190rpx;
		height: 64rpx;
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		border: 2rpx solid #C9CDD4;
		text-align: center;
		line-height: 64rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #4E5969;

	}

	.zsjm {
		width: 668rpx;
		overflow: hidden;
		position: relative;
		background: #fff;
		border-radius: 30rpx;


		.qdbox {
			width: 604rpx;
			height: 80rpx;
			background: linear-gradient(to top, #CD936A 0%, #F5C89D 100%);
			border-radius: 40rpx 40rpx 40rpx 40rpx;
			text-align: center;
			line-height: 80rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 28rpx;
			color: #FFFFFF;
			margin: auto;
			margin-top: 20rpx;
			margin-bottom: 50rpx;
		}

		.forms {
			width: 604rpx;
			margin: auto;
			margin-top: 30rpx;
			overflow: hidden;

			.linebox {
				width: 100%;
				display: flex;
				height: 84rpx;
				background: #F7F8FA;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				border: 2rpx solid #F7F8FA;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;

				.title1 {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #1D2129;
					margin-left: 28rpx;
				}

				.input {
					display: flex;
					align-items: center;
					margin-right: 20rpx;

					input {
						text-align: right;
						font-size: 24rpx;
					}

					.rightarrow {
						width: 24rpx;
						height: 24rpx;
					}
				}
			}
		}

		.title {
			width: 100%;
			text-align: center;
			margin-top: 48rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			font-size: 36rpx;
			color: #1D2129;
		}

		.closeicon {
			width: 44rpx;
			height: 44rpx;
			position: absolute;
			right: 24rpx;
			top: 24rpx;
		}
	}

	::v-deep .u-popup__content {
		background-color: rgba(255, 0, 0, 0) !important;
	}

	.msgicon {
		width: 608rpx;
		margin: 100rpx auto;
		overflow: hidden;

		.msgtext {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #86909C;
			text-align: center;
			width: 100%;
			margin-top: 30rpx;
		}

		image {
			width: 608rpx;
		}
	}

	.activecolor {
		color: #2AB4F5 !important;
	}



	.itemboxss {
		width: 686rpx;
		margin: auto;
		margin-top: 30rpx;



		.itembox {
			width: 686rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.18);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin-bottom: 28rpx;
			overflow: hidden;

			&_center {
				width: 630rpx;
				margin: auto;
				overflow: hidden;

				.bottomline {
					width: 100%;
					overflow: hidden;
					height: 124rpx;
					border-top: 1rpx solid #C9CDD4;
					display: flex;
					align-items: center;
					margin-top: 28rpx;
					justify-content: flex-end;

					.borderbtn {
						width: 190rpx;
						height: 64rpx;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						border: 2rpx solid #C9CDD4;
						text-align: center;
						line-height: 64rpx;
						font-weight: 400;
						font-size: 28rpx;
						color: #4E5969;
					}

					.btn {
						width: 178rpx;
						height: 64rpx;
						background: #EEF9FF;
						border-radius: 8rpx 8rpx 8rpx 8rpx;
						text-align: center;
						line-height: 64rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: bold;
						font-size: 28rpx;
						color: #2AB4F5;
					}
				}

				.spinfo {
					width: 100%;
					overflow: hidden;
					margin-top: 36rpx;
					display: flex;
					align-items: center;

					image {
						width: 156rpx;
						height: 156rpx;
						border-radius: 16rpx 16rpx 16rpx 16rpx;
					}

					.text1 {
						width: 452rpx;
						margin-left: 16rpx;
						overflow: hidden;

						.l1 {
							display: flex;
							align-items: center;
							width: 100%;

							.bt1 {
								width: 100%;
								display: -webkit-box;
								-webkit-box-orient: vertical;
								-webkit-line-clamp: 1;
								overflow: hidden;
								font-family: PingFang SC, PingFang SC;
								font-weight: 500;
								font-size: 28rpx;
								color: #1D2129;
							}

							.box {
								width: 48rpx;
								height: 32rpx;
								background: #7EBC79;
								border-radius: 4rpx 16rpx 4rpx 16rpx;
								font-family: PingFang SC, PingFang SC;
								font-weight: 500;
								font-size: 24rpx;
								color: #FFFFFF;
								text-align: center;
								line-height: 32rpx;
								margin-right: 8rpx;
							}
						}

						.l2 {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #7F7F7F;
							overflow: hidden;
							word-break: break-all;
							white-space: nowrap;
							margin-top: 18rpx;
							width: 408rpx;
						}

						.yajinbox {
							display: flex;
							align-items: center;
							margin-top: 15rpx;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #7F7F7F;
							justify-content: space-between;

							.tihuofs {
								font-family: PingFang SC, PingFang SC;
								font-weight: 400;
								font-size: 28rpx;
								color: #F53F3F;
							}
						}
					}
				}

				.topline {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 28rpx;



					.dingdanbinhao {
						width: 80%;
						word-break: break-all;
						/* 或 */
						display: flex;
						align-items: center;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 26rpx;
						color: #4E5969;

						&_icon {
							width: 32rpx;
							height: 32rpx;
						}
					}

					.statustext {
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 28rpx;
						white-space: nowrap;
						color: #FF7613;
					}
				}
			}
		}
	}

	.orderList {
		width: 100%;
		overflow: hidden;
		position: relative;

		.orderListback {
			width: 100%;
			height: 664rpx;
			background: linear-gradient(180deg, #CAEEFF 16%, rgba(240, 250, 255, 0) 100%);
			position: absolute;
			z-index: -1;
		}

		.seachbox {
			width: 686rpx;
			height: 68rpx;
			background: rgba(255, 255, 255, 0.65);
			border-radius: 34rpx 34rpx 34rpx 34rpx;
			margin: auto;
			overflow: hidden;
			margin-top: 6rpx;
			display: flex;
			align-items: center;
			position: relative;

			.lefticon {
				margin-left: 20rpx;
				width: 32rpx;
				height: 32rpx;
			}

			input {
				margin-left: 16rpx;
				font-size: 24rpx;
			}

			.seachboxbtn {
				width: 112rpx;
				height: 44rpx;
				line-height: 44rpx;
				text-align: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				position: absolute;
				color: #1D2129;
				border-left: 2rpx solid #86909C;
				right: 0%;
			}

			.closeicon {
				width: 44rpx;
				height: 44rpx;
				position: absolute;
				right: 150rpx;
			}
		}

		.classifybox {
			width: 100%;
			margin: auto;
			margin-left: 32rpx;
			white-space: nowrap;
			margin-top: 40rpx;

			.itembox {
				width: 96rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 32rpx;
				color: #1D2129;
				position: relative;
				margin-bottom: 28rpx;
				display: flex;
				flex-direction: column;
				align-items: center;

				.activebox {
					width: 48rpx;
					height: 6rpx;
					background: #2AB4F5;
					border-radius: 4rpx 4rpx 4rpx 4rpx;
					position: absolute;
					bottom: -70%;
					margin-top: 10rpx;
				}
			}
		}

	}
</style>